package orif.android.tutorials;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.Toast;

public class Tutorials extends Activity implements OnClickListener {

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		// Add event listeners
		findViewById(R.id.btnLinearLayout).setOnClickListener(this);
		findViewById(R.id.btnRelativeLayout).setOnClickListener(this);
		findViewById(R.id.btnTableLayout).setOnClickListener(this);
		findViewById(R.id.btnGridView).setOnClickListener(this);

	}

	/** Called when a click happens. */
	public void onClick(View v) {
		if (v == findViewById(R.id.btnLinearLayout)) {
			setContentView(R.layout.linearlayout);

			// Add event listeners
			findViewById(R.id.btnBack).setOnClickListener(this);

		} else if (v == findViewById(R.id.btnRelativeLayout)) {
			setContentView(R.layout.relativelayout);

			// Add event listeners
			findViewById(R.id.btnBack).setOnClickListener(this);

		} else if (v == findViewById(R.id.btnTableLayout)) {
			setContentView(R.layout.tablelayout);

			// Add event listeners
			findViewById(R.id.btnBack).setOnClickListener(this);

		} else if (v == findViewById(R.id.btnGridView)) {
			setContentView(R.layout.gridview);

			GridView gridview = (GridView) findViewById(R.id.gridview);
			gridview.setAdapter(new ImageAdapter(this));

			gridview.setOnItemClickListener(new OnItemClickListener() {
				public void onItemClick(AdapterView<?> parent, View v,
						int position, long id) {
					Toast.makeText(Tutorials.this, "" + position,
							Toast.LENGTH_SHORT).show();
				}
			});

			// Add event listeners
			findViewById(R.id.btnBack).setOnClickListener(this);

		} else if (v == findViewById(R.id.btnBack)) {
			setContentView(R.layout.main);

			// Add event listeners
			findViewById(R.id.btnLinearLayout).setOnClickListener(this);
			findViewById(R.id.btnRelativeLayout).setOnClickListener(this);
			findViewById(R.id.btnTableLayout).setOnClickListener(this);
			findViewById(R.id.btnGridView).setOnClickListener(this);
		}
	}
}